package com.fuyao.cloud.personnel.problem.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.DictItemOption;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.entity.ProblemInfor;
import com.fuyao.cloud.personnel.api.vo.ProblemInforSearchVO;
import com.fuyao.cloud.personnel.api.vo.excel.ProblemInforExcelVO;
import com.fuyao.cloud.personnel.api.vo.excel.ProblemInforExportExcelVO;
import com.fuyao.cloud.personnel.problem.service.ProblemInforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 问题管理
 *
 * @author pigx code generator
 * @date 2022-04-26 10:13:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/infor" )
@Api(value = "infor", tags = "问题管理管理")
public class ProblemInforController {

    private final ProblemInforService problemInforService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param problemInfor 问题管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('problem_infor_view')" )
    public R getFyProblemInforPage(Page page, ProblemInforSearchVO problemInfor) {
        QueryWrapper queryWrapper = SpliceQueryWrapper.spliceSearchCondition(problemInfor);
        queryWrapper.orderByDesc("CREATE_TIME");
        return R.ok(problemInforService.page(page, queryWrapper));
    }

    /**
     * 用于组件的查询
     * @param page 分页对象
     * @param problemInfor 问题管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/component/page" )
//    @PreAuthorize("@pms.hasPermission('problem_infor_component_page')" )
    public R getInforComponentPage(Page page, ProblemInforSearchVO problemInfor) {
        return R.ok(problemInforService.page(page, SpliceQueryWrapper.spliceSearchCondition(problemInfor)));
    }


    /**
     * 通过id查询问题管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_infor_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(problemInforService.getById(id));
    }

    /**
     * 新增问题管理
     * @param problemInfor 问题管理
     * @return R
     */
    @ApiOperation(value = "新增问题管理", notes = "新增问题管理")
    @SysLog("新增问题管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('problem_infor_add')" )
    public R save(@RequestBody ProblemInfor problemInfor) {
        return R.ok(problemInforService.save(problemInfor));
    }

    /**
     * 修改问题管理
     * @param problemInfor 问题管理
     * @return R
     */
    @ApiOperation(value = "修改问题管理", notes = "修改问题管理")
    @SysLog("修改问题管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('problem_infor_edit')" )
    public R updateById(@RequestBody ProblemInfor problemInfor) {
        return R.ok(problemInforService.updateById(problemInfor));
    }

    /**
     * 通过id删除问题管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除问题管理", notes = "通过id删除问题管理")
    @SysLog("通过id删除问题管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_infor_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(problemInforService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param problemInfor 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('problem_infor_export')" )
    public List<ProblemInforExportExcelVO> export(ProblemInfor problemInfor) {
        return problemInforService.export(Wrappers.query(problemInfor));
    }

    /**
     * 导入
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('problem_infor_export')")
    public R importProblemInfor(@RequestExcel List<ProblemInforExcelVO> excelVOList, BindingResult bindingResult) {
        return problemInforService.importProblemInfor(excelVOList, bindingResult);
    }

    @GetMapping("/onlyNum")
    @PreAuthorize("@pms.hasPermission('problem_infor_edit')" )
    public R<String> getOnlyNum() {
        return R.ok(problemInforService.getOnlyNum());
    }

    @GetMapping("/updateStatus/{id}/{status}")
    @PreAuthorize("@pms.hasPermission('problem_infor_edit')" )
    public R updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status) {
        return problemInforService.updateStatus(id,status);
    }

    @ApiOperation(value = "通过单号查询", notes = "通过单号查询")
    @GetMapping("/number/{number}" )
    public R getNumber(@PathVariable("number" ) String number) {
        LambdaQueryWrapper<ProblemInfor> queryWrapper = Wrappers.<ProblemInfor>lambdaQuery().eq(ProblemInfor::getNumber, number);
        return R.ok(problemInforService.getOne(queryWrapper, false));
    }

    @ApiOperation(value = "获取问题来源", notes = "获取问题来源")
    @PostMapping("/getSource" )
    public R getSource() {
        List<ProblemInfor> problemInforList = problemInforService.list();
        if(CollUtil.isNotEmpty(problemInforList)){
            List<String> sourceList = problemInforList.stream().map(ProblemInfor::getSource).distinct().collect(Collectors.toList());
            List<DictItemOption> resultSourceList = Lists.newArrayList();
            sourceList.forEach(s -> {
                if(StrUtil.isNotEmpty(s)){
                    resultSourceList.add(DictItemOption.builder().label(s).value(s).build());
                }
            });
            return R.ok(resultSourceList,"SUCCESS");
        }
        return R.ok();
    }

}
